package com.b3434.Processor;

import org.apache.avro.generic.GenericRecord;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Lee on 2017/3/23 0023.
 */
public class RegexProcessor extends Processor{
    Processor processor;
    public RegexProcessor(Processor processor) {
        this.processor = processor;
        this.parameter=processor.parameter;
    }
    @Override
    public GenericRecord process(String originStr) {
        dataMap=processor.process(originStr);
        List<String> regexs=this.parameter.getFieldRegex();
        List<String> fieldNameRegexed = this.parameter.getFiledNameRegexed();
        Pattern pattern;
        Matcher matcher;

        for (int i=0;i<regexs.size();i++) {
            pattern = Pattern.compile(regexs.get(i));
            matcher = pattern.matcher(originStr);
            if(matcher.find())
            this.dataMap.put(fieldNameRegexed.get(i),matcher.group(0));
        }
        return this.dataMap;
    }
}
